package com.prdiot.poetry.controller;

import com.prdiot.poetry.domains.Author;
import com.prdiot.poetry.domains.Poems;
import com.prdiot.poetry.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Optional;

@Controller
public class PoetryController {

    @Autowired
    PoemsService poemsService;

    @Autowired
    TagsService tagsService;

    @Autowired
    ChengyuService chengyuService;

    @Autowired
    MingjuService mingjuService;

    @Autowired
    AuthorService authorService;

    @GetMapping("/poetry/{id}")
    public String poetry(Model model,@PathVariable("id") String id){
        Poems poems = poemsService.searchPoemsByCode(id);

        model.addAttribute("poem",poems);
        model.addAttribute("tagList",tagsService.getTagsOrderByPoemCount(100));
        model.addAttribute("mingjuList",mingjuService.getRandMingju());
        model.addAttribute("chengyuList",chengyuService.getRandPoetry());
        if (poems!=null && poems.getAuthorId()!=null && !poems.getAuthorId().equals("null") && poems.getAuthorId().length()>0){
            model.addAttribute("author",authorService.findByAuthorId(poems.getAuthorId()));
            model.addAttribute("authorPoems",poemsService.attachTag(poemsService.getRandPoetryByAuthorId(poems.getAuthorId())));
        }

        return "poetry";
    }


}
